package db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import form.AccountForm.AccountForm;

import other.CodeDescriptionPair;

public class Account {
	
	private String username;
	private String password;
	private String retypePassword;
	private String firstName;
	private String lastName;
	private CodeDescriptionPair acctType;
	

	
	public String getAcctTypeCode() {
		return acctType.getCode();
	}
	
	public void setAcctTypeCode(String acctTypeCode) {
		acctType.setCode(acctTypeCode);
	}
	public String getAcctTypeDesc() {
		return acctType.getDescription();
	}
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getRetypePassword() {
		return retypePassword;
	}
	public void setRetypePassword(String retypePassword) {
		this.retypePassword = retypePassword;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	private Account(ResultSet rs){
		try{
			username = rs.getString("username");
			password = rs.getString("password");
			firstName = rs.getString("FName");
			lastName = rs.getString("LName");
			acctType = new CodeDescriptionPair(rs.getString("UserType"), CodeDescriptionPair.TABLE_USER_TYPES);
			
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	public static Account createAccount(String username){
		String query = "SELECT * FROM useraccounts WHERE username = '" + username + "';";
		Connection con = DBConnectionFactory.getInstance().getConnection();
		ResultSet rs = DBConnector.executeQuery(con, query);
		
		Account newAccount = null;
		
		try {
			if(rs != null && rs.next())
			{
				newAccount = new Account(rs);
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	
		
		try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return newAccount;
	}
	
	public Account getCopy(){
		Account copy = createAccount(username);
		
		copy.setFirstName(firstName);
		copy.setLastName(lastName);
		copy.setPassword(password);
		copy.setAcctTypeCode(getAcctTypeCode());
		
		return copy;
	}

	public static Account createAccountFromUsername(String selectedUsername) {
		System.out.println(selectedUsername);
		String query = "SELECT * FROM useraccounts WHERE username = '"+ selectedUsername+"';";

		Connection con = DBConnectionFactory.getInstance().getConnection();
		ResultSet rs = DBConnector.executeQuery(con, query);

		Account newAccount = null;
		
	
			try {
				if(rs != null && rs.next())
				{
					newAccount = new Account(rs);
				}
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	
		
		try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return newAccount;
	}

	public void updateSelfinDB(Account newAccount) {		
			String query = "UPDATE useraccounts SET usertype = '" + newAccount.getAcctTypeCode() + "' WHERE username = '"+ newAccount.username + "'";
			Connection con = DBConnectionFactory.getInstance().getConnection();	
			DBConnector.executeUpdate(con, query);
			
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	
	
	public static void insertIntoDB(AccountForm registerForm){
		String query = "INSERT INTO useraccounts(Username, Password, FName, LName, UserType) " +
				" VALUES ('"+registerForm.getUserName().getInput().trim()+"' , '"+registerForm.getPassword().getInput().trim()+"' ," +
						" '"+registerForm.getAccountInfoForm().getFName().getInput().trim()+"', '"+registerForm.getAccountInfoForm().getLName().getInput().trim()+"' , 'NEW')";
		try {
			Connection con = DBConnectionFactory.getInstance().getConnection();
			DBConnector.executeUpdate(con, query);
			con.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}


